﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PDAMain.Entity;
using PDAMain.Interface;

namespace PDAMain.DAL.Dao
{
    class SynchronizationDataDao : BaseDao
    {
        /// <summary>
        /// ���ͬ��(�ϴ�δͬ���ɹ�����)
        /// </summary>
        public void UploadData()
        {
            try
            {
                string CustomerSql = "select CWOrder_No,status,updatedt,Comment from CustomerWork where status!='1'";
                //string CustomerSql = "select * from CustomerWork where status !='1'";
                string InspectionSql = "select ASSIGNMENTID,status,STATUSDATE,remark,STARTTIME,ENDTIME from InspectionWork where status!='1'";
                string AmmeterSql = "select MPRId,EM_METER_ID,READDEGREE,READINGTIME from ELECTRICITYMETER where READDEGREE>0";
                string WaterSql = "select MPRId,WM_METER_ID,READDEGREE,READINGTIME from WATERMETER where READDEGREE>0";
                //string RepairSql = "select WONUM,status,remark,STATUSDATE from repairWorkList where status !='1'";
                string RepairSql = "select ASSIGNMENTID,WONUM,status,remark,STATUSDATE,STARTTIME,ENDTIME from repairWorkList where status not in ('1','4')";
                string ApplyRepairSql = "select BARCODE ,RECORDE_ID ,APPLY_TIME , REMARK , REASON,WORKSTATUS,LOCATEDID,ESTDUR from APPLYTOREPAIR";
                //同步上传资产备件信息
                try
                {
                    Execute_RepairAssetInfo();
                }
                catch { }
                //同步上传保修信息
                try
                {
                    DataTable ApplyRepair = base.executeDataSet(ApplyRepairSql).Tables[0];
                    Execute_ApplyRepair(ApplyRepair);
                }
                catch { }
                //同步上传客服工单信息
                try
                {
                    DataTable Customer = base.executeDataSet(CustomerSql).Tables[0];
                    Execute_Customer(Customer);
                }
                catch { }
                //同步上传巡检工单信息
                try
                {
                    DataTable Inspection = base.executeDataSet(InspectionSql).Tables[0];
                    Execute_Inspection(Inspection);
                }
                catch { }
                //同步上传电表信息
                if (NetWorkState.checkWifiState())
                {
                    try
                    {
                        DataTable Ammeter = base.executeDataSet(AmmeterSql).Tables[0];
                        Execute_Ammeter(Ammeter);
                    }
                    catch { }
                    //同步上传水表信息
                    try
                    {
                        DataTable Water = base.executeDataSet(WaterSql).Tables[0];
                        Execute_Water(Water);
                    }
                    catch { }
                }
                //同步上传维修工单信息
                try
                {
                    DataTable Repair = base.executeDataSet(RepairSql).Tables[0];
                    Execute_Repair(Repair);
                }
                catch { }
            }
            catch (Exception ex)
            {

            }
        }

        //�ͬ���ͷ���
        public void Execute_Customer(DataTable dt)
        {
            pdaWebReference.PdaService pda = base.getPdaWebService();
            for (int index = 0; index < dt.Rows.Count; index++)
            {
                string customerID = dt.Rows[index]["CWOrder_No"].ToString().Trim();
                string status = dt.Rows[index]["status"].ToString().Trim();
                string time = dt.Rows[index]["updatedt"].ToString().Trim();
                string remark = dt.Rows[index]["Comment"].ToString().Trim();
                try
                {
                    bool Flag = pda.updateWorkOrder(customerID, status, time, remark, Identity.dutyPerson);
                    if (Flag)
                    {
                        string str_Sql = "delete from CustomerWork where CWOrder_No='" + customerID + "'";
                        Flag = base.executeNonQuery(str_Sql);
                    }
                }
                catch (Exception ex) { }
            }
        }

        //�ͬ��Ѳ�칤����
        public void Execute_Inspection(DataTable dt)
        {
            pdaWebReference.PdaService pda = base.getPdaWebService();
            for (int index = 0; index < dt.Rows.Count; index++)
            {
                string inspectionID = dt.Rows[index]["ASSIGNMENTID"].ToString().Trim();
                string status = dt.Rows[index]["status"].ToString().Trim();
                string time = dt.Rows[index]["STATUSDATE"].ToString().Trim();
                string remark = dt.Rows[index]["remark"].ToString().Trim();
                string starttime = dt.Rows[index]["STARTTIME"].ToString().Trim();
                string endtime = dt.Rows[index]["ENDTIME"].ToString().Trim();
                try
                {
                    bool Flag = pda.updateCheckWorkOrder(inspectionID, status, remark, time, Identity.dutyPerson, starttime, endtime);
                    if (Flag)
                    {
                        string str_Sql = "delete from InspectionWork where ASSIGNMENTID='" + inspectionID + "'";
                        Flag = base.executeNonQuery(str_Sql);
                        if (Flag)
                        {
                            str_Sql = "delete from inspectionWorkDetail where ASSIGNMENTID='" + inspectionID + "'";
                            base.executeNonQuery(str_Sql);
                        }
                    }
                }
                catch (Exception ex) { }
            }
        }

        //�ͬ�������
        public void Execute_Ammeter(DataTable dt)
        {
            pdaWebReference.PdaService pda = base.getPdaWebService();
            for (int index = 0; index < dt.Rows.Count; index++)
            {
                string MPRId = dt.Rows[index]["MPRId"].ToString().Trim();
                string EM_METER_ID = dt.Rows[index]["EM_METER_ID"].ToString().Trim();
                string degree = dt.Rows[index]["READDEGREE"].ToString().Trim();
                string completedt = dt.Rows[index]["READINGTIME"].ToString().Trim();
                try
                {
                    bool blFlag = pda.updateAmmeterReadingWorkOrder(MPRId, degree, completedt, Identity.dutyPerson);
                    if (blFlag)
                    {
                        string delsql = " DELETE FROM ELECTRICITYMETER WHERE EM_METER_ID='" + EM_METER_ID + "'";
                        bool delFlag = base.executeNonQuery(delsql);
                    }
                }
                catch (Exception ex) { }
            }
        }

        //�ͬ��ˮ����
        public void Execute_Water(DataTable dt)
        {
            pdaWebReference.PdaService pda = base.getPdaWebService();
            for (int index = 0; index < dt.Rows.Count; index++)
            {
                string MPRId = dt.Rows[index]["MPRId"].ToString().Trim();
                string WM_METER_ID = dt.Rows[index]["WM_METER_ID"].ToString().Trim();
                string degree = dt.Rows[index]["READDEGREE"].ToString().Trim();
                string completedt = dt.Rows[index]["READINGTIME"].ToString().Trim();
                try
                {
                    bool blFlag = pda.updateWatermeterReadingWorkOrder(MPRId, degree, completedt, Identity.dutyPerson);
                    if (blFlag)
                    {
                        string delsql = " DELETE FROM WATERMETER WHERE WM_METER_ID='" + WM_METER_ID + "'";
                        //ݸ��³ɹ��h��ӛ
                        bool delFlag = base.executeNonQuery(delsql);
                    }
                }
                catch (Exception ex) { }
            }
        }

        //�ͬ��ά�޹�����
        public void Execute_Repair(DataTable dt)
        {
            pdaWebReference.PdaService pda = base.getPdaWebService();
            for (int index = 0; index < dt.Rows.Count; index++)
            {
                string assignmentId = dt.Rows[index]["ASSIGNMENTID"].ToString().Trim();
                string repairID = dt.Rows[index]["WONUM"].ToString().Trim();
                string status = dt.Rows[index]["status"].ToString().Trim();
                string remark = dt.Rows[index]["remark"].ToString().Trim();
                string starttime = dt.Rows[index]["STARTTIME"].ToString().Trim();
                string endtime = dt.Rows[index]["ENDTIME"].ToString().Trim();
                try
                {
                    //bool Flag = pda.updateRepairWorkOrder(repairID, status, remark, Identity.dutyPerson, starttime, endtime);
                    bool Flag = pda.updateRepairWorkOrder(assignmentId, status, remark, Identity.dutyPerson, starttime, endtime);
                    if (Flag)
                    {
                        string str_Sql = "delete from repairWorkList where ASSIGNMENTID='" + assignmentId + "'";
                        //ݸ��³ɹ��h��ӛ
                        bool delFlag = base.executeNonQuery(str_Sql);
                    }
                }
                catch (Exception ex) { }
            }
        }

        //�ͬ���ʲ����Ϣ
        public void Execute_RepairAssetInfo()
        {
            DataTable dt = DataHelper.RepairWorkDao.GetRepairAssetInfo();
            if (dt.Rows.Count > 0)
            {
                pdaWebReference.PdaService pda = base.getPdaWebService();
                for (int index = 0; index < dt.Rows.Count; index++)
                {
                    try
                    {
                        string wonum = dt.Rows[index]["WONUM"].ToString().Trim();
                        string barcode = dt.Rows[index]["BARCODE"].ToString().Trim();
                        string detail = dt.Rows[index]["DETAIL"].ToString().Trim();
                        string person = dt.Rows[index]["ACCOUNT"].ToString().Trim();
                        string sparepartId = dt.Rows[index]["sparepartId"].ToString().Trim();
                        string starttime = dt.Rows[index]["STARTTIME"].ToString().Trim();
                        string endtime = dt.Rows[index]["ENDTIME"].ToString().Trim();
                        string remark = dt.Rows[index]["REMARK"].ToString().Trim();
                        DataHelper.RepairWorkDao.insertMaterial(sparepartId, wonum, barcode, person, detail);
                        bool result = pda.insertMaterial(sparepartId, wonum, barcode, person, detail);
                        if (result)
                        {
                            string sql = "delete from RepairAsswet where sparepartId='" + sparepartId + "'";
                            base.executeNonQuery(sql);
                            bool Flag = pda.updateRepairWorkOrder(sparepartId, "4", remark, Identity.dutyPerson, starttime, endtime);
                            if (Flag)
                            {
                                string str_Sql = "delete from repairWorkList where ASSIGNMENTID='" + sparepartId + "'";
                                //ݸ��³ɹ��h��ӛ
                                bool delFlag = base.executeNonQuery(str_Sql);
                            }
                        }
                    }
                    catch
                    { }
                }
            }
        }

        public void Execute_ApplyRepair(DataTable ApplyRepair)
        {
            if (ApplyRepair.Rows.Count > 0)
            {
                pdaWebReference.PdaService pda = base.getPdaWebService();
                foreach (DataRow dr in ApplyRepair.Rows)
                {
                    string barCode = dr["BARCODE"].ToString().Trim();
                    string recordeId = dr["RECORDE_ID"].ToString().Trim();
                    string applyTime = dr["APPLY_TIME"].ToString().Trim();
                    string remark = dr["REMARK"].ToString().Trim();
                    string reason = dr["REASON"].ToString().Trim();
                    string status = dr["WORKSTATUS"].ToString().Trim();
                    string locatedid = dr["LOCATEDID"].ToString().Trim();
                    string estdur = dr["ESTDUR"].ToString().Trim();
                    bool result = pda.insertApplyRepair(barCode, recordeId, applyTime, remark, reason, locatedid, status, estdur);
                    if (result)
                    {
                        string sql = "delete from APPLYTOREPAIR where BARCODE='" + barCode + "'";
                        base.executeNonQuery(sql);
                    }
                }
            }
        }
    }
}